package com.lbj.mall.member.controller;

import com.lbj.mall.common.entity.vo.GiteeVo;
import com.lbj.mall.common.exception.AppException;
import com.lbj.mall.common.utils.PageUtils;
import com.lbj.mall.common.utils.R;
import com.lbj.mall.member.entity.MemberEntity;
import com.lbj.mall.member.entity.vo.MemberLoginVo;
import com.lbj.mall.member.entity.vo.MemberRegisterVo;
import com.lbj.mall.member.exception.EmailExistException;
import com.lbj.mall.member.exception.UsernameExistException;
import com.lbj.mall.member.feign.MemberFeign;
import com.lbj.mall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author libingjun
 * @email 617416791@qq.com
 * @date 2022-03-27 12:09:32
 */
@RefreshScope
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberFeign memberFeign;

    @Value("${teacher.name}")
    String name;
    @Value("${teacher.age}")
    String age;

    @PostMapping("/gitee")
    public R giteeLogin(@RequestBody GiteeVo giteeVo) {
        MemberEntity member = memberService.giteeLogin(giteeVo);
        return R.ok().put("data", member);
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo loginVo) {
        MemberEntity member = memberService.login(loginVo);
        if (member == null) {
            return R.error(AppException.USERNAME_PASSWORD_ERROR.getCode(),
                    AppException.USERNAME_PASSWORD_ERROR.getMsg());
        }
        return R.ok().put("data", member);
    }

    /**
     * 配置中心测试方式
     */
    @RequestMapping("/getConfig")
    public R getConfig() {
        return R.ok().put("name", name)
                .put("age", age);
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo registerVo) {
        try {
            memberService.register(registerVo);
        } catch (UsernameExistException e) {
            return R.error(AppException.USERNAME_EXIST.getCode(), AppException.USERNAME_EXIST.getMsg());
        } catch (EmailExistException e) {
            return R.error(AppException.EMAIL_EXIST.getCode(), AppException.EMAIL_EXIST.getMsg());
        }
        return R.ok();
    }

    /**
     * 获取远程优惠券信息
     */
    @RequestMapping("/getCoupon")
    public R getCoupon() {
        MemberEntity member = new MemberEntity();
        member.setNickname("小明");
        member.setCity("杭州");
        R test = memberFeign.test();
        return R.ok().put("member", member)
                .put("coupon", test.get("coupon"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
